package com.mhtube.controller;

import com.alibaba.fastjson.JSONObject;
import com.mhtube.entity.constant.CodeConstant;
import com.mhtube.entity.dto.StringDto;
import com.mhtube.entity.vo.ResponseVo;
import com.mhtube.service.UserService;
import com.mhtube.validate.UserInfoValidate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController("userController")
public class UserController {
    @Resource
    private UserService userService;

    // 注册用户
    @GetMapping("/registerUser")
    public ResponseVo registerUser(StringDto dto) {
        ResponseVo vo = new ResponseVo();
        String valiResult = UserInfoValidate.registerValidate(dto);
        if(valiResult != null) {
            vo.setStatus(CodeConstant.registerErrorCode);
            vo.setInfo("请求失败");
            vo.setData(valiResult);
            return vo;
        }
        String result = userService.registerUser(dto.getUsername(), dto.getPassword(), dto.getEmail(), dto.getNikeName());
        if (result != null) {
            vo.setStatus(CodeConstant.registerErrorCode);
            vo.setInfo("请求失败");
            vo.setData(result);
        } else {
            vo.setStatus(CodeConstant.successCode);
            vo.setInfo("请求成功");
        }
        return vo;
    }

    // 登录用户
    @GetMapping("/loginUser")
    public ResponseVo loginUser(HttpSession session, HttpServletResponse response, StringDto dto) {
        ResponseVo vo = new ResponseVo();
        String valiResult = UserInfoValidate.loginValidate(dto);
        if(valiResult != null) {
            vo.setStatus(CodeConstant.loginErrorCode);
            vo.setInfo("登录失败");
            vo.setData(valiResult);
            return vo;
        }
        boolean result = userService.loginUser(session, response, dto.getUsername(), dto.getPassword());
        if(result) {
            vo.setStatus(CodeConstant.successCode);
            vo.setInfo("登录成功");
        } else {
            vo.setStatus(CodeConstant.loginErrorCode);
            vo.setInfo("登录失败");
        }
        return vo;
    }

    // 获取用户基本信息
    @GetMapping("/getBasicUserInfo")
    public ResponseVo getBasicUserInfo(HttpSession session) {
        ResponseVo vo = new ResponseVo();
        JSONObject data = userService.getBasicUserInfo(session);
        if(data == null) {
            vo.setStatus(CodeConstant.infoErrorCode);
            vo.setInfo("信息获取失败");
            return vo;
        }
        vo.setStatus(CodeConstant.successCode);
        vo.setInfo("信息获取成功");
        vo.setData(data);
        return vo;
    }

    // 修改个人描述
    @GetMapping("/modifyDescription")
    public ResponseVo modifyDescription(HttpSession session, StringDto dto) {
        ResponseVo vo = new ResponseVo();
        if(dto.getDescription() == null || dto.getDescription().length() == 0) {
            vo.setStatus(CodeConstant.modifyErrorCode);
            vo.setInfo("个人描述修改失败");
            return vo;
        }
        boolean result = userService.modifyDescription(session, dto.getDescription());
        if(result) {
            vo.setStatus(CodeConstant.successCode);
            vo.setInfo("个人描述修改成功");
        } else {
            vo.setStatus(CodeConstant.modifyErrorCode);
            vo.setInfo("个人描述修改失败");
        }
        return vo;
    }

    // 修改昵称
    @GetMapping("/modifyNikeName")
    public ResponseVo modifyNikeName(HttpSession session, StringDto dto) {
        ResponseVo vo = new ResponseVo();
        if(dto.getNikeName() == null || dto.getNikeName().length() == 0) {
            vo.setStatus(CodeConstant.modifyErrorCode);
            vo.setInfo("昵称修改失败");
            return vo;
        }
        boolean result = userService.modifyNikeName(session, dto.getNikeName());
        if(result) {
            vo.setStatus(CodeConstant.successCode);
            vo.setInfo("昵称修改成功");
        } else {
            vo.setStatus(CodeConstant.modifyErrorCode);
            vo.setInfo("昵称修改失败");
        }
        return vo;
    }

    // 关注用户
    @GetMapping("/subscribeUser")
    private ResponseVo subscribeUser(HttpSession session, StringDto dto) {
        ResponseVo vo = new ResponseVo();
        if(dto.getSubscribeId() == null || dto.getSubscribeId().length() == 0) {
            vo.setStatus(CodeConstant.subscribeErrorCode);
            vo.setInfo("关注失败");
            return vo;
        }
        boolean result = userService.subscribeUser(session, dto.getSubscribeId());
        if(result) {
            vo.setStatus(CodeConstant.successCode);
            vo.setInfo("关注成功");
        } else {
            vo.setStatus(CodeConstant.subscribeErrorCode);
            vo.setInfo("关注失败");
        }
        return vo;
    }
}
